<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 15.2.&nbsp; Pattern Matching</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-CHP-15-SECT-2"></a>
<h3 id="title-IDAQH2EF" class="docSection1Title">15.2. Pattern Matching</h3>
<a name="IDX-CHP-15-0665"></a> 
<a name="IDX-CHP-15-0666"></a> 

<a name="learnphpmysql-CHP-15-SECT-2.1"></a>
<h4 id="title-IDACI2EF" class="docSection2Title">15.2.1. </h4>
<p class="docText"><span class="docEmphasis">Pattern matching</span> allows you to build expressions that match strings using a specific matching syntax called a <span class="docEmphasis">regular expression</span>. Regular expressions allow you to perform searching tasks such as separating out a certain tag for an incoming text file, or validating user input such as email addresses.</p>
<p class="docText">The easiest way to use regular expressions<a name="IDX-CHP-15-0667"></a> 
<a name="IDX-CHP-15-0668"></a> 
<a name="IDX-CHP-15-0669"></a> 
 in PHP is to use the PCRE (Perl-compatible regular expressions) extension. This extension is installed by default, so it should be part of your PHP environment. PHP also supports a style of regular expression matching functions called <tt>ereg</tt> that are older and less compatible than PCRE functions.</p>
<p class="docText">A regular expression is really just a string. The string uses a combination of special characters and literals to allow matching of other strings. For example, the following string describes an email address:</p>
<pre>
\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b
</pre><br>

<p class="docText">It does this by searching for:</p>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">Sequential alphanumeric and punctuation characters, which form the username</p></div></li><LI><div style="font-weight:normal"><p class="docList">The at symbol (<tt>@</tt>)</p></div></li><LI><div style="font-weight:normal"><p class="docList">A group of alphanumeric and punctuation characters, which forms the first part of the domain name</P></div></li><LI><div style="font-weight:normal"><p class="docList">A period, which separates the domain name from the extension</p></div></li><li><div style="font-weight:normal"><p class="docList">A two- to four-character alpha string, which signifies the top level domainfor example, <tt>com</tt> and <tt>net</tt></P></div></li></ol></div>
<p class="docText">The descriptors used in the regular expression are:</P>
<dl class="docList">
<dt><br><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">\b</span></span></span></P></dt>
<dd><p class="docList">A boundary point of a word</P></dd>

<dt><br><P><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">[aAbB]</span></span></span></p></dt>
<dd><p class="docList">One of anything inside the brackets: <tt>a, A, b, B</tt></p></dd>

<dt><BR><P><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">{2,4}</span></span></span></p></dt>
<dd><p class="docList">A total of between 2 and 4 of anything preceding the brackets</p></dd>

<dt><br><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">A-Z</span></span></span></P></dt>
<dd><p class="docList">Any letter between <tt>A</tt> and <tt>Z</tt>, such as <tt>A, B</tt>, and <tt>C</tt></p></dd>

<dt><BR><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">\.</span></span></span></p></dt>
<dd><p class="docList">A literal period</P></dd>

<dt><BR><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">+</span></span></span></P></dt>
<dd><p class="docList">Match the preceding block one or more times</p></dd>
</dl>
<p class="docText">There are two types of characters in the regular expression string. Those that match themselves, such as the at (<tt>@</tt>) symbol, are called <span class="docEmphasis">literals</span>, meaning they literally match. The other type is called <span class="docEmphasis">metacharacters</span>, which describe matching by specifying repetition, ranges, and combinations within the expression.</p>
<a name="learnphpmysql-CHP-15-SECT-2.1.1"></a>
<h5 id="title-IDAPM2EF" class="docSection3Title">15.2.1.1. Quantifiers</h5>
<a name="IDX-CHP-15-0670"></a> 
<a name="IDX-CHP-15-0671"></a> 

<p class="docText"><span class="docEmphasis">Quantifiers</span> are metacharacters that specify how many times you wish to match the preceding pattern in a string.</p>
<p class="docText">Quantifiers include:</p>
<dl class="docList">
<dt><br><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">*</span></span></span></p></dt>
<dd><p class="docList">Zero or more</p></dd>

<dt><br><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">+</span></span></span></P></dt>
<dd><p class="docList">One or more</p></dd>

<dt><br><P><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">?</span></span></span></P></dt>
<dd><p class="docList">Zero or one</p></dd>

<dt><BR><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">{</span></span> <tt><i>num</i></tt><span class="docPubcolor"><span class="docMonofont">}</span></span> </span></P></dt>
<dd><p class="docList">Exactly <tt><i>num</I></tt> times</p></dd>

<dt><br><P><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">{</span></span> <tt><I>num</i></tt><span class="docPubcolor"><span class="docMonofont">,}</span></span> </span></P></dt>
<dd><p class="docList">At least <tt><i>num</i></tt> times</P></dd>

<dt><BR><p><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">{</span></span> <tt><i>min,max</i></tt><span class="docPubcolor"><span class="docMonofont">}</span></span> </span></p></dt>
<dd><p class="docList">At least <tt><I>min</i></tt> but not more than <tt><I>max</i></tt> times</p></dd>
</dl>
<p class="docText">For example, the regular expression <tt>[a-f]?ex</tt> matches both <tt>alex</tt> and <tt>ex</tt>, but not <tt>ax</tt>.</P>

<a name="learnphpmysql-CHP-15-SECT-2.1.2"></a>
<H5 id="title-IDA0P2EF" class="docSection3Title">15.2.1.2. Anchors</h5>
<a name="IDX-CHP-15-0672"></a> 
<a name="IDX-CHP-15-0673"></a> 

<p class="docText">Anchors define a specific location for a match to take place. To match the start of a line, the caret character (<tt>^</tt>) is used. To match the end of a line, the dollar character (<tt>$</tt>) is used. To match a string that begins with <tt>I</tt>, use the regular expression <tt>^I</tt>.</P>
<p class="docText">Other anchors<a name="IDX-CHP-15-0674"></a> 
<a name="IDX-CHP-15-0675"></a> 
 deal with <span class="docEmphasis">word boundaries</span>. Words are made up of consecutive letters, digits, and underscores. All other characters, such as spaces, punctuation, and newline characters, are word boundaries. To match a word boundary, the backslash b (<tt>\b</tt>) character is used. To match everywhere that isn't a word boundary, the backslash capital B (<tt>\B</tt>) character is used. <a class="docLink" href="#learnphpmysql-CHP-15-TABLE-1">Table 15-1</a> lists other word boundaries.</p>
<a name="learnphpmysql-CHP-15-TABLE-1"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 15-1. Escaped word boundaries</h5></caption><colgroup span="2"><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Character</p></th><th class="thead" scope="col" align="left"><p class="docText">Anchor type</p></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText"><tt>\b</tt></p></td><TD class="docTableCell" align="left"><p class="docText">A word boundary</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText"><tt>\B</tt></P></td><td class="docTableCell" align="left"><p class="docText">A nonword boundary</p></TD></tr><TR><td class="docTableCell" align="left"><p class="docText"><tt>\d</tt></p></TD><TD class="docTableCell" align="left"><p class="docText">A single digit character</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText"><tt>\D</tt></P></td><td class="docTableCell" align="left"><p class="docText">A single nondigit character</p></td></TR><tr><TD class="docTableCell" align="left"><p class="docText"><tt>\n</tt></p></td><TD class="docTableCell" align="left"><p class="docText">The newline character</P></td></TR><tr><td class="docTableCell" align="left"><p class="docText"><tt>\r</tt></p></td><td class="docTableCell" align="left"><p class="docText">The carriage return character</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>\s</tt></p></td><TD class="docTableCell" align="left"><p class="docText">A single whitespace character</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText"><tt>\S</tt></P></td><td class="docTableCell" align="left"><p class="docText">A single nonwhitespace character</p></TD></tr><TR><td class="docTableCell" align="left"><p class="docText"><tt>\t</tt></p></TD><TD class="docTableCell" align="left"><p class="docText">The tab character</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText"><tt>\w</tt></P></td><td class="docTableCell" align="left"><p class="docText">A single word character, alphanumeric and underscore</p></td></TR><tr><TD class="docTableCell" align="left"><p class="docText"><tt>\W</tt></p></td><TD class="docTableCell" align="left"><p class="docText">A single nonword character</P></td></TR></table></p><br>

<a name="learnphpmysql-CHP-15-SECT-2.1.3"></a>
<h5 id="title-IDAZ42EF" class="docSection3Title">15.2.1.3. Character classes</h5>
<p class="docText">A <span class="docEmphasis">character class</span> allows you to group several characters together and work with them in a regular expression as though they were one character. Use the square brackets (<tt>[]</tt>) to group the characters together. For example, to match any alpha character twice:</p>
<pre>
[a-zA-Z]{2}
</pre><br>

<p class="docText">You can also use a <span class="docEmphasis">negated character class</span>,<a name="IDX-CHP-15-0676"></a> 
 which selects the opposite of the character class by adding a caret (<tt>^</tt>) character after the opening square bracket. Note that this is the only time that caret character doesn't represent an anchor. The following matches all nonalpha characters.</p>
<pre>
[^a-zA-Z]
</pre><br>


<a name="learnphpmysql-CHP-15-SECT-2.1.4"></a>
<h5 id="title-IDAW52EF" class="docSection3Title">15.2.1.4. Executing pattern matches in PHP</h5>
<p class="docText">PHP uses a set of functions that start with <tt>preg_</tt> to perform regular expression operations on strings. These functions take a regular expression as a parameter in a string format. There are functions for doing a variety of operations on strings, including splitting them up and returning matching portions.</p>
<p class="docText">The regular expression string must be in Perl format, which specifies that the regular expression start with <tt>'/</tt> and end with /<tt>'</tt>. The regular expression goes between the single quote and slashes, as in <tt>'/</tt><tt><i>regular expression</I></tt><tt>/'</tt>. Forward slashes in the expression must be escaped with a backslash. For example, <span class="docEmphasis">/home/example</span> becomes <tt>'/\/home\/example/'</tt>.</p>
<p class="docText">To specify regular expression options such as case insensitivity, add the parameter to the end of the regex string after the last slash. These most common parameters are listed in <a class="docLink" href="#learnphpmysql-CHP-15-TABLE-2">Table 15-2</a>.</p>
<a name="learnphpmysql-CHP-15-TABLE-2"></a><P><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><H5 class="docTableTitle">Table 15-2. Regular expression characters</h5></caption><colgroup span="2"><col><col></colgroup><thead><TR><th class="thead" scope="col" align="left"><p class="docText">Regex character</p></th><th class="thead" scope="col" align="left"><p class="docText">Meaning</p></th></tr></thead><TR><td class="docTableCell" align="left"><p class="docText"><tt>s</tt></P></td><td class="docTableCell" align="left"><p class="docText">Dot matches all characters</P></TD></tr><TR><td class="docTableCell" align="left"><p class="docText"><tt>i</tt></p></TD><TD class="docTableCell" align="left"><p class="docText">Case insensitive</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText"><tt>m</tt></p></TD><td class="docTableCell" align="left"><p class="docText">Match start and end of line anchors at embedded new lines in the search string</p></TD></TR></table></p><BR>
<p class="docText">For example, use <tt>'/abc/i'</tt> to do a case-insensitive search of <tt>abc</tt>.</p>

<a name="learnphpmysql-CHP-15-SECT-2.1.5"></a>
<h5 id="title-IDAND3EF" class="docSection3Title">15.2.1.5. preg_match</h5>
<a name="IDX-CHP-15-0677"></a> 
<a name="IDX-CHP-15-0678"></a> 

<p class="docText">The function <tt>preg_match</tt> is used to return all matches based on the supplied regular expression and string. The function value returned is <tt>true</tt> if a match is found. Its syntax is:</p>
<pre>
preg_match (<tt><i>string pattern</i></tt>, <tt><i>string subject</i></tt> [, <tt><i>array groups</i></tt>])
</pre><br>

<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-15-EX-3">Example 15-3</a>, we search the string example to see if it has words that start with <tt>ple</tt>. Since the string doesn't start with <tt>ple</tt>, no results are returned.</P>
<a name="learnphpmysql-CHP-15-EX-3"></a><h5 id="title-IDAYE3EF" class="docExampleTitle">Example 15-3. Using preg_match to return an array of matches that start with ple</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
$subject = "example";
$pattern = '/^ple/';
preg_match($pattern, $subject, $matches);
print_r($matches);
?&gt;
</pre><BR>

</td></tr></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-15-EX-3">Example 15-3</a> displays:</P>
<pre>
Array ( )
</pre><br>




<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
